

package com.hazelcast.client.impl.protocol.task.management;

import com.hazelcast.client.impl.ClientEngine;
import com.hazelcast.client.impl.protocol.ClientMessage;
import com.hazelcast.client.impl.protocol.codec.MCSetDiagnosticsConfigCodec;
import com.hazelcast.client.impl.protocol.task.dynamicconfig.AbstractAddConfigMessageTask;
import com.hazelcast.config.Config;
import com.hazelcast.config.DiagnosticsConfig;
import com.hazelcast.config.DiagnosticsOutputType;
import com.hazelcast.instance.BuildInfo;
import com.hazelcast.instance.impl.Node;
import com.hazelcast.instance.impl.NodeExtension;
import com.hazelcast.internal.cluster.impl.ClusterServiceImpl;
import com.hazelcast.internal.nio.Connection;
import com.hazelcast.internal.serialization.InternalSerializationService;
import com.hazelcast.logging.ILogger;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
import com.hazelcast.security.permission.ManagementPermission;
import com.hazelcast.spi.impl.NodeEngine;

import java.security.Permission;

public class SetDiagnosticsConfigMessageTask extends AbstractAddConfigMessageTask<MCSetDiagnosticsConfigCodec.RequestParameters> {

    private static final Permission REQUIRED_PERMISSION = new ManagementPermission("cluster.setDiagnosticsConfig");

    public SetDiagnosticsConfigMessageTask(ClientMessage clientMessage, Node node, Connection connection) {
        super(clientMessage, node, connection);
    }

    public SetDiagnosticsConfigMessageTask(ClientMessage clientMessage, ILogger logger, NodeEngine nodeEngine, InternalSerializationService serializationService, ClientEngine clientEngine, Connection connection, NodeExtension nodeExtension, BuildInfo buildInfo, Config config, ClusterServiceImpl clusterService) {
        super(clientMessage, logger, nodeEngine, serializationService, clientEngine, connection, nodeExtension, buildInfo, config, clusterService);
    }

    @Override
    protected MCSetDiagnosticsConfigCodec.RequestParameters decodeClientMessage(ClientMessage clientMessage) {
        return MCSetDiagnosticsConfigCodec.decodeRequest(clientMessage);
    }

    @Override
    protected ClientMessage encodeResponse(Object response) {
        return MCSetDiagnosticsConfigCodec.encodeResponse();
    }

    @Override
    public Permission getRequiredPermission() {
        return REQUIRED_PERMISSION;
    }

    @Override
    @SuppressWarnings({"checkstyle:npathcomplexity", "checkstyle:cyclomaticcomplexity", "checkstyle:MethodLength"})
    protected IdentifiedDataSerializable getConfig() {
        DiagnosticsConfig diagnosticsConfig = new DiagnosticsConfig().setEnabled(parameters.enabled).setOutputType(DiagnosticsOutputType.valueOf(parameters.outputType)).setIncludeEpochTime(parameters.includeEpochTime).setMaxRolledFileSizeInMB(parameters.maxRolledFileSizeInMB).setMaxRolledFileCount(parameters.maxRolledFileCount).setLogDirectory(parameters.logDirectory).setFileNamePrefix(parameters.fileNamePrefix);

        diagnosticsConfig.getPluginProperties().putAll(parameters.properties);
        return diagnosticsConfig;
    }


    @Override
    public String getMethodName() {
        return "setDiagnosticsConfig";
    }

    @Override
    protected boolean checkStaticConfigDoesNotExist(IdentifiedDataSerializable config) {
        return true;
    }

    @Override
    public boolean isManagementTask() {
        return true;
    }
}
